package com.deke.home.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deke.entity.EduSubject;
import com.deke.home.dao.QianSubjectDao;
import com.deke.home.service.QianSubjectService;
import com.deke.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：Username 刘亦辰（59372312@qq.com）
 * @date ：Created in 2022/12/21 16:35
 * @description：
 * @modified By：
 * @version:
 **/

@Service
public class QianSubjectServiceImpl implements QianSubjectService {
    @Autowired
    private QianSubjectDao subjectDao;
    
    /**
     * @create by: Username 刘亦辰 
     * @description: 查找前台条形导航栏 
     * @create time: 2022/12/21 16:36
     * @param null 
     * @return :  
     **/
    @Override
    public Result getAllQianMenu() {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("status_id",1);
        List<EduSubject> subjectList = subjectDao.selectList(wrapper);
        List<EduSubject> first = new ArrayList<>();
        for (EduSubject subject : subjectList) {
            if (subject.getParentId().equals("0")){
                first.add(subject);
            }
        }

        for (EduSubject f : first) {
            f.setChildren(findChildren(subjectList,f.getId()));
        }
        return new Result(2000,"成功",first);
    }


    /**
     * @create by: Username 刘亦辰 
     * @description: 递归查询菜单 
     * @create time: 2022/12/21 16:43
     * @param null
     * @return :  
     **/
    private List<EduSubject> findChildren(List<EduSubject> eduSubjects, String id){
        List<EduSubject> senconds=new ArrayList<>();
        for (EduSubject p:eduSubjects){
            if(p.getParentId().equals(id)){
                senconds.add(p);
            }
        }
        for (EduSubject p:senconds){
            p.setChildren(findChildren(eduSubjects,p.getId()));
        }
        return senconds;
    }
}